package com.zhz.livegoods.login.redisdao.impl;

import com.zhz.livegoods.login.redisdao.ValidateCodeDao;
import com.zhz.livegoods.login.pojo.ValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
 * 用户登录验证码数据访问实现
 * @author zhz
 * @date 2020/08/08
 **/
@Service
public class ValidateCodeDaoImpl implements ValidateCodeDao {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    //保存数据到redis,有效期2分钟
    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key,value, Duration.ofMinutes(2));
        //redisTemplate.expire(key,2, TimeUnit.MINUTES);
    }

    //根据key查询value
    @Override
    public ValidateCode get(String key) {
        return (ValidateCode) redisTemplate.opsForValue().get(key);
    }

    //删除验证码
    @Override
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }
}
